package com.example.art.chapter1.test1

import android.content.Intent
import android.icu.lang.UCharacter.GraphemeClusterBreak.L
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import com.example.art.R
import kotlinx.android.synthetic.main.activity_my.*

/**
 * Created by 舍长
 * describe:测试Activity的生命周期
 * 1，启动onCreate() -> onStart() -> onResume()
 * 2, 返回系统界面—> onPause() -> onStop()
 * 3, 再次回到程序 onRestart() -> onStart() -> onResume()
 * 4, 使用系统返回键销毁Activity -> onPause() ->onStop -> onDestroy()
 */
class MyActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)
        Log.d("hello", "A-onCreate")
        btn_01.setOnClickListener(View.OnClickListener {
            val intent=Intent(this, SecondActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP// Intent.FLAG_ACTIVITY_NEW_TASK相当于为Activity指定singleTask模式
            startActivity(intent)
        })
        //恢复相应的数据
        if (savedInstanceState != null) {
            val text = savedInstanceState.getString("text")
            Log.d("hello", text)
        }
    }

    //保存
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
//        outState?.putString("text", edit.text.toString()+":特意加上的")
//        Log.d("hello", "onSaveInstanceState:要保存的数据：$edit.text.toString()")
    }

    //恢复
    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        //恢复数据
//        val text = savedInstanceState?.getString("text")
//        edit.setText(text)
        Log.d("hello", "onRestoreInstanceState")
    }

    override fun onStart() {
        super.onStart()
        Log.d("hello", "A-onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d("hello", "A-onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d("hello", "A-onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d("hello", "A-onStop")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d("hello", "A-onRestart")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("hello", "A-onDestroy")
    }
}
